package br.com.saeng2D.formulario.caixa_forca_c_m;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import br.com.saeng2D.formulario.caixa.ContainerAnimado;
import br.com.saeng2D.formulario.caixa.ControleAnimado;
import br.com.saeng2D.formulario.caixa.Dialogo;
import br.com.saeng2D.objeto.forca.ForcaMomento;
import br.com.saeng2D.util.TextFieldNumero;
import br.com.saeng2D.util.Util;

public class ControleContainerAnimadoForcaM extends ControleAnimado implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JLabel labelNM = new JLabel("N * m");
	private JTextField textFieldVariavelForca =  new TextFieldNumero(Short.MAX_VALUE);
	private JRadioButton radioHorario = new JRadioButton(Util.getString("label_horario"), true); 
	private JRadioButton radioAntiHorario = new JRadioButton(Util.getString("label_anti_horario")); 
	
	public ControleContainerAnimadoForcaM(String tituloObjetoAnimado, ContainerAnimado containerAnimado, Dialogo dialogo, int x, int y, int largura, int altura) {
		super(tituloObjetoAnimado, containerAnimado, dialogo);
		montarLayout(x, y, largura, altura);
		registrarEvento();
		setBorder(BorderFactory.createRaisedBevelBorder());
		registrarEventoVariavelForca();
	}

	public void montarLayout(int x, int y, int largura, int altura) {
		setLayout(null);
		setBounds(x, y, largura, altura);

		labelTitulo.setBounds(70, 5, 150, 15);
		add(labelTitulo);

		containerAnimado.setBounds(75, 20, 100, 100);
		add(containerAnimado);		
		textFieldVariavelForca.setBounds(48, altura - 70, largura - 100, 20);
		labelNM.setBounds(28 + largura - 75, altura - 70, 40, 20);
		add(textFieldVariavelForca);
		add(labelNM);
		ButtonGroup grupo = new ButtonGroup();
		grupo.add(radioHorario);
		grupo.add(radioAntiHorario);

		radioHorario.setBounds(35, altura - 35, 70, 30);
		radioHorario.setMargin(new Insets(0, 0, 0, 0));
		add(radioHorario);

		radioAntiHorario.setBounds(125, altura - 35, 100, 30);
		radioAntiHorario.setMargin(new Insets(0, 0, 0, 0));
		add(radioAntiHorario);
	}
	
	public void registrarEvento() {
		radioHorario.addActionListener(this);
		radioAntiHorario.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		JRadioButton radio = (JRadioButton) e.getSource();
		ForcaMomento forcaMomento = (ForcaMomento)containerAnimado.getAbstratoAnimado();  
		forcaMomento.setSentidoPosOuNeg(radio == radioHorario);
		containerAnimado.repaint();
	}
	
	public String getVariavelForca() {
		return textFieldVariavelForca.getText();
	}
	
	public void registrarEventoVariavelForca() {
		textFieldVariavelForca.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {
			}
			public void focusGained(FocusEvent e) {
				containerAnimado.selecionar();
			}
		});
		textFieldVariavelForca.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {
			}
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER) {
					dialogo.adicionarNaSuperficie();
				}
			}
			public void keyPressed(KeyEvent e) {
			}
		});
	}
}